import React from 'react'
// ===================================================================== Temp
import SearchForm from '#cpt/temp/SearchForm'
import Table from '#cpt/temp/Table'
import AddEditModal from '#cpt/temp/AddEditModal'
import TableControls from '#cpt/temp/TableControls'
import Controls from '#cpt/temp/Controls'
// ===================================================================== global declare
const { $fn, $Lazy } = window
// ===================================================================== Lazy
const Page 		= $Lazy.load(()=>import('#tp/page/page-container'))
const $cache 	= $Lazy.file(()=>import('#private/cache-api'))
// ===================================================================== data
const completeData = [{ name:'已完成', value:'1' }, { name:'未完成', value:'2' } ]
// ===================================================================== component
class Index extends React.Component{
	state = {
		data:[],
		pag: {},
		// 搜索表单列表
		forms : [
			{ label: '设备编号',		name: 'device_number' },
			{ label: '设备名称',		name: 'device_id',	type: 'select', required:true, half:true, width:250 },
		],
		// 提交表单列表
		formData : [
			{ label: '设备名称',		name: 'device_id',			type: 'select', required:true, half:true, onValue:({rows,formData})=>{
				if(rows){
					this.formRef.setValue({device_number:rows.device_number,device_model:rows.device_model,serial_man:rows.serial_man})
				}
				this.setState({formData})
				
			} },
			{ label: '设备编号',		name: 'device_number', 		half:true },
			{ label: '设备型号',		name: 'device_model', 		half:true, },
			{ label: '生产厂家',		name: 'serial_man', 		half:true, },
			{ label: '维护周期',		name: 'imple_cycle', 		half:true,  type:'number', required:true },
			{ label: '执行方',		name: 'imple_party', 		half:true },
			{ label: '执行情况',		name: 'imple_sit',			type: 'select', required:true, half:true, data: completeData },
			{ label: '维护内容',     name:'repair_content', 		full:true,	 type:'textarea' },
			{ label: '备注',     	name:'remark', 		full:true,	 type:'textarea' }
		]
	}
	
	model = {}
	
	componentDidMount(){
		this.uuid = $fn.query('uuid')
		if(this.uuid){ this.query = { device_id: this.uuid } }
		
		if(window.isInit){
			this.fetch()
		}
	}
	
	// 获取下拉数据
	getSelect = () => {
		const { forms, formData } = this.state
		
		$cache.then( async f => {
			// 设备名称
			
			$fn.setRows( [forms, formData], 'device_id', await f.getDivCache())
			
			// 设置设备默认值
			if(this.uuid){ this.searchFormRef.setValue({ device_id: this.uuid }) }
			
			this.setState({ forms })
		})
	}
	
	// paging
	fetch = () => $fn.fetch.call(this,'de-device-repair/index')
	
	// 表单
	cols = [
		{ title: '设备编号',		dataIndex: 'device_number', 	width:100, align:'center' },
		{ title: '设备名称', 	dataIndex: 'device_name',		width:200 },
		{ title: '设备型号', 	dataIndex: 'device_model', 		width:150 },
		{ title: '生产厂家', 	dataIndex: 'serial_man', 		width:150 },
		{ title: '维护周期', 	dataIndex: 'imple_cycle', 		width:80, 	align:'center' },
		{ title: '执行方', 		dataIndex: 'imple_party', 		width:100 },
		{ title: '执行人', 		dataIndex: 'imple_user', 		width:150 },
		{ title: '维护时间', 	dataIndex: 'repair_at', 		width:200 },
		{ title: '执行情况', 	dataIndex: 'imple_sit', 		width:100, 	align:'center', render:text=> $fn.getState(completeData,text) },
		{ title: '维护内容', 	dataIndex: 'repair_content', 	width:100 },
		{ title: '备注', 		dataIndex: 'remark', 			width:150 },
		TableControls.controls.call(this)
	]
	
	// 表格操作按钮
	table_controls = (text,{ uuid }) => [
		TableControls.edit.call(this, 'de-device-repair/info', { uuid }, { label:'维护登记', click: ()=>{
                    // 显示隐藏
                    $fn.hideRows(this.state.formData, ['imple_sit'], false)
                }})
	]
	
	// 操作按钮
	controls = () =>[
		Controls.add.call(this,{label:'维护计划', click: ()=>{
                const { formData } = this.state
                $fn.hideRows(formData, ['imple_sit'], true)
                this.setState({ formData })
            }}),
		Controls.pushTo.call(this,'设备维修','device-repair'),
		Controls.pushTo.call(this,'维修记录','device-repair-record'),
		this.uuid && Controls.back.call(this)
	]
	
	render(){
		return (
			<>
				<Page title='设备维护' controls={this.controls()}>
					{/* 搜索 */}
					{ SearchForm.call(this, { onForm: this.getSelect }) }
					{/* 表格 */}
					{ Table.call(this,{isInit:window.isInit}) }
				</Page>
				{/* 添加、编辑 */}
				{ AddEditModal.call(this,{addText:'维护计划', editText:'维护登记',  api: 'de-device-repair/add', editApi:'de-device-repair/edit' }) }
			</>
		)
	}
}

export default Index